package com.jadefashion.mobile;

import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.health.resources.ValidateCodeUtils;
import com.jadefashion.service.MemberService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    MemberService memberService;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("sendCode.do")
    public Object sendCode(@RequestParam("telephone") String telephone){
        Result result= null;

        try {
            String code = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
//            SMSUtils.sendCode(telephone,code);
            System.out.println("生成的验证码:"+code);
//            resource.setex(telephone+"_login",90,code);
            redisTemplate.opsForValue().set(telephone+"_login",code,90, TimeUnit.SECONDS);
            result = new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        return result;
    }

    @RequestMapping("validateCode.do")
    public Object validateCode(@RequestParam("telephone") String telephone,@RequestParam("code") String code){
        Result result= null;
        try {
//            String checkcode = resource.get(telephone+"_login");
            String checkcode = (String) redisTemplate.opsForValue().get(telephone + "_login");
            if(checkcode==null||checkcode.equals("")){
                result=new Result(false,"验证码超时");
            }else if(checkcode.equals(code)){
                result = new Result(true,"验证码正确");
            }else {
                result = new Result(false,"验证码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, "验证码校验失败");
        }
        return result;
    }

    @RequestMapping("login.do")
    public Object login(@RequestParam("telephone") String telephone,@RequestParam("code") String code){
        Result result= null;
//        Jedis resource = null;
        try {
//            resource=jedisPool.getResource();
//            String checkcode = resource.get(telephone+"_login");
            String checkcode= (String) redisTemplate.opsForValue().get(telephone+"_login");
            if(checkcode==null||checkcode.equals("")){
                result=new Result(false,"验证码超时");
            }else if(checkcode.equals(code)){
                memberService.addMemberByPhone(telephone);
                result = new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
            }else {
                result = new Result(false,"验证码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        return result;
    }
}
